package com.oriental.manage.controller.opcif;

import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.core.utils.SessionUtils;
import com.oriental.opcif.common.enums.type.AgreementTypeEnum;
import com.oriental.opcif.common.enums.type.EntityTypeEnum;
import com.oriental.opcif.common.model.RequestModel;
import com.oriental.opcif.common.model.Response;
import com.oriental.opcif.common.model.ResponseModel;
import com.oriental.opcif.common.util.BeanMapper;
import com.oriental.opcif.product.bizModel.manager.AgreementReportDto;
import com.oriental.opcif.product.bizModel.manager.TCustomerBankDto;
import com.oriental.opcif.product.bizModel.request.customerAgreement.IdentityAuthCheckReqDto;
import com.oriental.opcif.product.bizModel.request.customerAgreement.IdentityAuthQueryReqDto;
import com.oriental.opcif.product.bizModel.response.customerAgreement.AuthApplyResDto;
import com.oriental.opcif.product.bizModel.response.customerAgreement.SignAgreementResDto;
import com.oriental.opcif.product.bizModel.response.customerAgreement.UserIdentityAuthResDto;
import com.oriental.opcif.product.facade.IdentityAuthFacade;
import com.oriental.paycenter.commons.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 类注释
 *
 * @author: Chuch
 * @date: 2017/2/28
 * @time: 11:04
 * @see: 链接到其他资源
 * @since: 1.0
 * 客户管理-身份认证
 */
@Slf4j
@Controller
@RequestMapping("opcif/idcardAuthentication")
public class OpcifIDCardAuthenticationController {

    @Autowired
    IdentityAuthFacade identityAuthFacade;

    @RequestMapping("init")
    public String init() {
        return "opcif/opcifIDCardAuthentication";
    }

    /**
     * 身份认证列表查询
     */
    @RequestMapping("search")
    @ResponseBody
    public ResponseDTO<Pagination<AuthApplyResDto>> queryPage(@RequestBody Pagination<IdentityAuthQueryReqDto> query) {
        log.info("身份认证请求参数:{}", query);
        ResponseDTO<Pagination<AuthApplyResDto>> responseDTO = new ResponseDTO<Pagination<AuthApplyResDto>>();
        try {
            RequestModel<IdentityAuthQueryReqDto> requestModel = new RequestModel<IdentityAuthQueryReqDto>();
            requestModel.setPageNo(query.getPageNum());
            requestModel.setPageSize(query.getPageSize());
            requestModel.setRequest(query.getQueryBean());

            Pagination<AuthApplyResDto> page =new Pagination<>();
            page.setPageNum(query.getPageNum());
            page.setPageSize(query.getPageSize());
            ResponseModel<AuthApplyResDto> responseModel= identityAuthFacade.query(requestModel);
            if (responseModel.getResponseResult()) {
                page.setRowCount(responseModel.getTotal());
                page.setList(responseModel.getList());
                responseDTO.setSuccess(true);
                responseDTO.setObject(page);
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg(responseModel.getResponseMessage());
            }
/*          AuthApplyResDto authApplyResDto = new AuthApplyResDto();
            authApplyResDto.setAuthApplyNo("Z001");
            authApplyResDto.setOperatorNo("00001");
            authApplyResDto.setCustomerNo("110");
            authApplyResDto.setCertNo("36043019921125191X");
            authApplyResDto.setCertName("王二");
            authApplyResDto.setAuthStatus("01");
            authApplyResDto.setDescription("未认证");
            authApplyResDto.setAuthChannel("App");
            authApplyResDto.setApplyDate(new Date());
            List<AuthApplyResDto> list = new ArrayList<>();
            list.add(authApplyResDto);
            query.setSuccess(true);
            query.setResult(list);
            if (query.isSuccess()) {
                pagination.setList(query.getResult());
                responseDTO.setSuccess(true);
                responseDTO.setObject(pagination);
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg("查询失败");
            }*/
        } catch (Exception e) {
            log.error("查询失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return responseDTO;
    }


    /**
     * 身份认证审核
     */
    @RequestMapping("toDetail")
    public String toDetail() {
        return "opcif/opcifIDCardAuthenticationDetail";
    }

    /**
     * 身份认证单条记录查询
     */
    @RequestMapping("selectOneMsg")
    @ResponseBody
    public ResponseDTO<UserIdentityAuthResDto> selectOneMsg(AuthApplyResDto authApplyResDto) {
        log.info("身份认证单条记录查询入参:{}", authApplyResDto);
        ResponseDTO<UserIdentityAuthResDto> responseDTO = new ResponseDTO<>();
        try {
            Response<UserIdentityAuthResDto> response = identityAuthFacade.queryAuthInfo(authApplyResDto.getAuthApplyId());
            responseDTO.setCode(response.getResponseCode());
            responseDTO.setMsg(response.getResponseDesc());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject(response.getResult());
            } else {
                responseDTO.setSuccess(false);
            }
        } catch (Exception e) {
            log.error("单条记录查询失败：", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("查询失败");
        }
        return responseDTO;
    }

    /**
     * 身份认证审批
     */
    @RequestMapping("toAuth")
    @ResponseBody
    public ResponseDTO<SignAgreementResDto> auth(@RequestBody  IdentityAuthCheckReqDto model) {
        log.info("身份认证审批入参:{}", model);
        ResponseDTO<SignAgreementResDto> responseDTO = new ResponseDTO<>();
        try {
            model.setAgreementType(AgreementTypeEnum.AGREEMENT_TYPE_IDENTITY_AUTH.getCode());
            model.setEntityType(EntityTypeEnum.ENTITY_NAME_OPERATOR.getCode());
            model.setUpdatedBy(SessionUtils.getUserName());
            Response<SignAgreementResDto> response = identityAuthFacade.auth(model);
            responseDTO.setCode(response.getResponseCode());
            responseDTO.setMsg(response.getResponseDesc());
            if (response.isSuccess()) {
                responseDTO.setSuccess(true);
                responseDTO.setObject(response.getResult());
            } else {
                responseDTO.setSuccess(false);
            }
        } catch (Exception e) {
            log.error("身份认证审批失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("审批失败");
        }
        return responseDTO;
    }
}
